set MySQL global in GCP & search SQL


Posted by kscheng on 2021-04-06

一般作法

set global global_log = on;

遭遇錯誤:沒有 Super 權限。
然而,在 GCP(Google Could Platform)管理的條件下,沒有人有 Super 權限。

到 GCP 用 flag 設定

以 sql admin 身分進行以下動作。

在搜尋欄輸入 SQL 後看到運行中的 Instance,逐一點選進入。

先到 dev-comebet,細看右下方的「設定」。

點選「編輯設定」(此畫面為設定完成後之狀態)。

下拉畫面後點選「旗標」,「新增旗標」。

在「新增旗標」中找尋目標項目。這裡的設定是:
SET global log_output = 'table';
SET global general_log = on;

點選完畢後,要按下「完成」後並「儲存」。

回到主頁後,點選「重新啟動」才算完成!

點選主畫面的「所有執行個體」,回到步驟 2,進入 dev-sports,重複上述步驟。

回到 MySQL client,查看狀態。
show variables like '%log';

查詢 general_log 表格內容。

use mysql
select *
from general_log
where command_type = 'Query'
-- limit 11

更改 argument 的欄位顯示方式。

use mysql
select  user_host, server_id, convert(argument using 'utf8mb4') sql_text
from general_log
where command_type = 'Query'
-- order by length(argument) desc
-- limit 11

-- 找出最常用的 SQL Syntax
select convert(argument using 'utf8mb4') sql_text
from mysql.general_log
where command_type = 'Prepare'
group by convert(argument using 'utf8mb4')
order by length(convert(argument using 'utf8mb4'))

查詢優化的幾個重點:

  • 設計並善用 index,用 explain 觀察使用狀況。
  • 以 limit 來限制輸出量
  • 先查小表再查大表

參考資料:

MySQL索引
20條Tips:高性能SQL查詢,優化取數速度方案


#SQL #Prepare







Related Posts

HTML 基礎

HTML 基礎

The introduction and difference between class component and function component in React

The introduction and difference between class component and function component in React

Leetcode: Geometry Problems

Leetcode: Geometry Problems


Comments